--
-- Author: yawei.zhu
-- Date: 2018-07-03 10:58:05
--
-- OtherPlayerController
--

local OtherPlayerView = import("..views.OtherPlayerView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    -- self._model = app:getInst("OtherPlayerModel")
    self._model = app:getInst("PlayerModel")

    EM:on("onWatchUI", function(name, uid)
        self._model:reqWatchOtherUser(uid)
    end)
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end

function ClassRef:openView(data)
    if cc.isDead(self._view) then
        self._view = OtherPlayerView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        self:addChild(self._view, data)
    end

    -- 观察别人，需要关闭聊天界面
    app:sendMsg("IMRoomController", "hideView")

    self._view:show()
    self._otherUserInfo = data
    self._view:updateView(data)
end

function ClassRef:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end

function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

-- 显示装备tip
function ClassRef:_showEquipTip( render )
    if not self:isViewAlive() then return end

    local equipData = render:getData()
    local param = {}
    param.itemId = equipData.tid
    param.dataTemp = equipData
    param.tipsType = Enums.TipsType.equip
    param.unShowOrigin = true -- 不显示来源
    param.unShowBind = true -- 不显示绑定状态

    -- 以下6个参数在 tipsType == Enums.TipsType.equip 时必须有内容!! 默认取UD
    -- puid                 -- player uid
    -- equipstrengthGrid    -- player 的装备格子强化等级列表
    -- equips               -- player 的装备列表
    -- class                -- player 的职业
    -- level                -- player 的等级
    -- specialistId         -- player 的专精ID

    param.puid = equipData.uid
    param.equipstrengthGrid = self._otherUserInfo.equipGridStrengths
    param.equips = self._otherUserInfo.equips
    param.class = self._otherUserInfo.baseInfo.classID
    param.level = self._otherUserInfo.baseInfo.level
    param.specialistId = self._otherUserInfo.baseInfo.specialistID

    param.closeCallBack = function()
        -- print("IconRender.removeTip")
        if not tolua.isnull( render ) then
            render:setSelected(false)
        end
    end

    local boundingBoxTemp = render:getBoundingBox()
    local worldPosTemp = render:getParent():convertToWorldSpace( cc.p( boundingBoxTemp.x, boundingBoxTemp.y ) )
    boundingBoxTemp.x = worldPosTemp.x
    boundingBoxTemp.y = worldPosTemp.y
    param.boundingBox = boundingBoxTemp

    app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=param})
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("OtherPlayerController:onOperate op = " .. op)
    if op == "PlayerPanel_showEquipTip" then -- 人物身上装备显示装备tip
        self:_showEquipTip( data )
    end
end

return ClassRef
